<?php
namespace App\BaseExt;
use App\BaseExt\JPush\Client;
use App\BaseExt\JPush\Exceptions\APIConnectionException;
use App\BaseExt\JPush\Exceptions\APIRequestException;

/**
 * PicUpload Class
 */
class JPush {
    private $app_key = '033fd00d3165c6657cfee9a5';
    private $master_secret = '86614ac75eb834c87cc2a214';

    public function pushTag($tag = "duobao95",$data="")
    {

        $client = new Client($this->app_key, $this->master_secret);
        $push_payload = $client->push()
            ->setPlatform('all')
            ->addTag($tag)
            ->message('这里是我写滴标题', [
                'title' => '这他妈又是一个标题',
//                'content_type' => 'text',
                'extras' => [
                    'key' =>$data
                ]
            ])
//            ->setNotificationAlert('setNotificationAlert')
        ;
        try {
            $response = $push_payload->send();
//            print_r($response);
        } catch (APIConnectionException $e) {
            // try something here
            print $e;//todo 上线的时候应该关掉
        } catch (APIRequestException $e) {
            // try something here
            print $e;//todo 上线的时候应该关掉
        }
    }


    public function pushShopOrUser($title,$content,$isShop,$data = array()){

        $client = new Client($this->app_key, $this->master_secret);
        if ($isShop == 0) {
            $tag = "user";
        } else {
            $tag = "shop";
        }

        $push_payload = $client->push()
            ->setPlatform('all')
            ->addTag($tag)
            ->message($content, [
                'content_type' => 'text',
                'extras' => $data
            ])
            ->setNotificationAlert($title)
        ;
        try {
            $response = $push_payload->send();
        } catch (APIConnectionException $e) {
            // try something here
            print $e;//todo 上线的时候应该关掉
        } catch (APIRequestException $e) {
            // try something here
            print $e;//todo 上线的时候应该关掉
        }
    }

    public function pushShop($title,$content,$data = array())
    {
        $this->pushShopOrUser($title,$content,1,$data);
    }

    public function pushUser($title,$content,$data = array())
    {
        $this->pushShopOrUser($title,$content,0,$data);
    }



    public function pushToOneUser($uid,$title,$content = "",$data = array()){
        $client = new Client($this->app_key, $this->master_secret);
        $push_payload = $client->push()
            ->setPlatform('all')
                ->addAlias("p$uid")
            ->setNotificationAlert($title)
        ;
        var_dump($push_payload);
        try {
            $response = $push_payload->send();
            var_dump($response);
        } catch (APIConnectionException $e) {
//            print $e;//todo 上线的时候应该关掉
        } catch (APIRequestException $e) {
//            print $e;//todo 上线的时候应该关掉
        }
    }

    public function pushToOneShop($uid,$title,$content = "",$data = array()){
        $client = new Client("3f1793df889e4952f36994c5", "ba08b6f5a53aaaae8095ef88");
        $push_payload = $client->push()
            ->setPlatform('all')
                ->addAlias("p$uid")
            ->setNotificationAlert($title)
        ;
        try {
            $response = $push_payload->send();
        } catch (APIConnectionException $e) {
//            print $e;//todo 上线的时候应该关掉
        } catch (APIRequestException $e) {
//            print $e;//todo 上线的时候应该关掉
        }
    }

}
